#!/usr/bin/perl
######################################################################
# Script for combining 2 plots into a single postscript document
# -- this generates a latex script based on the command line inputs
#    and several hardcoded variables. The latex script is then run to
#    generate the postscript output.
######################################################################

######################################################################
# PAGE LAYOUT ....
#
# |-------|
# | FILE1 |
# |       |
# | FILE2 |
# |-------|
#
#
######################################################################


######################################################################
# HARDCODED VARIABLES
######################################################################
$PLOT_NAME_ROOT="data_clean";
$FILE1_ROOT="PLOT_data";
$FILE2_ROOT="PLOT_clean";
######################################################################

$numArgs = $#ARGV + 1;
print STDERR "---------------------------------------------\n";
print STDERR "ARG1 (DIR)       = $ARGV[0]\n";
print STDERR "ARG2 (ID)        = $ARGV[1]\n";
print STDERR "---------------------------------------------\n";
#USAGE INFO
if ($numArgs != 2) {
    print STDERR "ERROR: command line args, ($numArgs/2)\n";
    print STDERR "USAGE: PLOT_data_clean_1x2.pl [DIR] [ID]\n"; 
    print STDERR "------------------------------------------------\n";
    print STDERR "
#
# |-------|
# | FILE1 |
# |       |
# | FILE2 |
# |-------|
#\n";
    exit(1);
}

$DIR=$ARGV[0];
$ID=$ARGV[1];
$FILE1="$FILE1_ROOT-${ID}.ps";
$FILE1_PATH="$DIR/$FILE1";
$FILE2="$FILE2_ROOT-${ID}.ps";
$FILE2_PATH="$DIR/$FILE2";
$PLOT_NAME="$PLOT_NAME_ROOT-$ID";

$PLOT_TEX="${PLOT_NAME}.tex";
$PLOT_DVI="${PLOT_NAME}.dvi";
$PLOT_PS="PLOT_${PLOT_NAME}.ps";
$PLOT_LOG="${PLOT_NAME}.log";
$PLOT_AUX="${PLOT_NAME}.aux";
$PLOT_INFO="${PLOT_NAME}.info";
$PLOT_RUN="${PLOT_NAME}.run";

print STDERR "=============================================\n";
print STDERR "DIR      = $DIR\n";
print STDERR "FILE1    = $FILE1\n";
print STDERR "FILE2    = $FILE2\n";
print STDERR "=============================================\n";
print STDERR "TEX FILE = $PLOT_TEX\n";
print STDERR "DVI FILE = $PLOT_DVI\n";
print STDERR "LOG FILE = $PLOT_LOG\n";
print STDERR "AUX FILE = $PLOT_AUX\n";
print STDERR "INFO FILE= $PLOT_INFO\n";
print STDERR "---------------------------------------------\n";
print STDERR "PS  FILE = $PLOT_PS\n";
print STDERR "=============================================\n";
print STDERR "RUN FILE = $PLOT_RUN\n";
print STDERR "=============================================\n";

print STDERR "=-> writing info file to ${DIR}/${PLOT_INFO}\n";
open(INFOFILE,">${DIR}/${PLOT_INFO}");
print INFOFILE "1(DIR)     = ${DIR}\n";
print INFOFILE "2(ID)      = ${ID}\n";
print INFOFILE "=(FILE1)   = ${FILE1}\n";
print INFOFILE "=(FILE2)   = ${FILE2}\n";
print INFOFILE "=(NAME)    = ${PLOT_NAME}\n";
print INFOFILE "=(RUN FILE)= ${PLOT_RUN}\n";
print INFOFILE "=(PS FILE) = ${PLOT_PS}\n";
close(INFOFILE);

open(TFILE,">${DIR}/${PLOT_TEX}");

print TFILE "\\documentclass[11pt,a4paper]{article}\n";
print TFILE "%-----------------------------------------------------\n";
print TFILE "% \n";
print TFILE "% latex file for:\n";
print TFILE "% DIR: $DIR\n";
print TFILE "% FILE1: $FILE1\n";
print TFILE "% FILE2: $FILE2\n";
print TFILE "% \n";
print TFILE "% |--------|\n";
print TFILE "% | FILE 1 |\n";
print TFILE "% |        |\n";
print TFILE "% | FILE 2 |\n";
print TFILE "% |--------|\n";
print TFILE "%\n";
print TFILE "%-----------------------------------------------------\n";
print TFILE "%Margin setup                     \n";
print TFILE "\\setlength{\\voffset}{0.1in}     \n";
print TFILE "\\setlength{\\headheight}{0in}    \n";
print TFILE "\\setlength{\\headsep}{0in}       \n";
print TFILE "\\setlength{\\textheight}{9.8in}  \n";
print TFILE "\\setlength{\\topmargin}{-1in}  \n";
print TFILE "\\setlength{\\topskip}{0in}       \n";
print TFILE "\\setlength{\\oddsidemargin}{-1in}  \n";
print TFILE "\\setlength{\\evensidemargin}{-0.4in} \n";
print TFILE "\\pagestyle{empty}  %no page numbering\n";
print TFILE "\\usepackage{graphics} \n";
print TFILE "\\usepackage{sidecap} \n";
print TFILE "%---------------------------------------------------- \n";
print TFILE "%\n";
print TFILE "\\begin{document} \n";
print TFILE "%\n";
print TFILE "%--------------------------------------------------- \n";
print TFILE "\\begin{figure} \n";
print TFILE "\\begin{center} \n";
print TFILE "\\rotatebox{270}{\\resizebox{140mm}{!}{\\includegraphics{$DIR/$FILE1}}} \n";
print TFILE "\\rotatebox{270}{\\resizebox{140mm}{!}{\\includegraphics{$DIR/$FILE2}}} \n";
#print TFILE "\\caption{$CAPTION)} \n";
print TFILE "\\end{center} \n";
print TFILE "\\end{figure} \n";
print TFILE "%----------------------------------------------------------- \n";
print TFILE "%\n";
print TFILE "\\end{document} \n";
close(TFILE);

open(RUNFILE,">${DIR}/${PLOT_RUN}");
print RUNFILE "latex ${DIR}/${PLOT_TEX}\n";
print RUNFILE "dvips ${PLOT_DVI}\n";
print RUNFILE "mv ${PLOT_NAME}.ps $DIR/${PLOT_PS}\n";
print RUNFILE "rm ${PLOT_LOG} ${PLOT_AUX} ${PLOT_DVI}\n";
print RUNFILE "echo \"-------------------------------------------------\"\n";
print RUNFILE "echo \"gv $DIR/${PLOT_PS} &\"\n";
print RUNFILE "echo \"-------------------------------------------------\"\n";
close(RUNFILE);

print STDERR "================================================\n";
print STDERR "-Done creating latex script!\n";
print STDERR "-Running the script via the runfile\n";
system("sh ${DIR}/${PLOT_RUN}");
print STDERR "-plot complete\n";
print STDERR "================================================\n";


